package com.dianping.cat.consumer.matrix.model.transform;

import com.dianping.cat.consumer.matrix.model.IEntity;
import com.dianping.cat.consumer.matrix.model.IVisitor;
import com.dianping.cat.consumer.matrix.model.entity.Matrix;
import com.dianping.cat.consumer.matrix.model.entity.MatrixReport;
import com.dianping.cat.consumer.matrix.model.entity.Ratio;
import java.lang.reflect.Array;
import java.text.SimpleDateFormat;
import java.util.Collection;
import java.util.Date;
import java.util.Map;


















public class DefaultXmlBuilder
  implements IVisitor
{
  private IVisitor m_visitor;
  private int m_level;
  private StringBuilder m_sb;
  private boolean m_compact;
  
/*  39 */   public DefaultXmlBuilder() { this(false); }



/*  43 */   public DefaultXmlBuilder(boolean compact) { this(compact, new StringBuilder(4096)); }
  
  public DefaultXmlBuilder(boolean compact, StringBuilder sb) {
    this.m_visitor = this;
/*  47 */     this.m_compact = compact;
/*  48 */     this.m_sb = sb;
/*  49 */     this.m_sb.append("<?xml version=\"1.0\" encoding=\"utf-8\"?>\r\n");
  }
  
  public String buildXml(IEntity<?> entity) {
/*  53 */     entity.accept(this.m_visitor);
/*  54 */     return this.m_sb.toString();
  }
  
  protected void endTag(String name) {
/*  58 */     this.m_level--;

/*  60 */     indent();
/*  61 */     this.m_sb.append("</").append(name).append(">\r\n");
  }

  
/*  65 */   protected String escape(Object value) { return escape(value, false); }

  
  protected String escape(Object value, boolean text) {
/*  69 */     if (value == null) {
/*  70 */       return null;
    }
    
/*  73 */     String str = toString(value);
/*  74 */     int len = str.length();
/*  75 */     StringBuilder sb = new StringBuilder(len + 16);

/*  77 */     for (int i = 0; i < len; i++) {
      char ch = str.charAt(i);
      
/*  80 */       switch (ch) {
        case '<':
/*  82 */           sb.append("&lt;");
          break;
        case '>':
/*  85 */           sb.append("&gt;");
          break;
        case '&':
/*  88 */           sb.append("&amp;");
          break;
        case '"':
/*  91 */           if (!text) {
/*  92 */             sb.append("&quot;");
            break;
          }
        default:
/*  96 */           sb.append(ch);
          break;
      }
    
    }
/* 101 */     return sb.toString();
  }
  
  protected void indent() {
/* 105 */     if (!this.m_compact) {
/* 106 */       for (int i = this.m_level - 1; i >= 0; i--) {
/* 107 */         this.m_sb.append("   ");
      }
    }
  }

  
/* 113 */   protected void startTag(String name) { startTag(name, false, null, new Object[0]); }



/* 117 */   protected void startTag(String name, boolean closed, Map<String, String> dynamicAttributes, Object... nameValues) { startTag(name, null, closed, dynamicAttributes, nameValues); }



/* 121 */   protected void startTag(String name, Map<String, String> dynamicAttributes, Object... nameValues) { startTag(name, null, false, dynamicAttributes, nameValues); }

  
  protected void startTag(String name, Object text, boolean closed, Map<String, String> dynamicAttributes, Object... nameValues) {
/* 125 */     indent();

/* 127 */     this.m_sb.append('<').append(name);

/* 129 */     int len = nameValues.length;

/* 131 */     for (int i = 0; i + 1 < len; i += 2) {
/* 132 */       Object attrName = nameValues[i];
/* 133 */       Object attrValue = nameValues[i + 1];

/* 135 */       if (attrValue != null) {
/* 136 */         this.m_sb.append(' ').append(attrName).append("=\"").append(escape(attrValue)).append('"');
      }
    }
    
/* 140 */     if (dynamicAttributes != null) {
/* 141 */       for (Map.Entry<String, String> e : dynamicAttributes.entrySet()) {
/* 142 */         this.m_sb.append(' ').append((String)e.getKey()).append("=\"").append(escape(e.getValue())).append('"');
      }
    }
    
/* 146 */     if (text != null && closed) {
/* 147 */       this.m_sb.append('>');
/* 148 */       this.m_sb.append(escape(text, true));
/* 149 */       this.m_sb.append("</").append(name).append(">\r\n");
    } else {
/* 151 */       if (closed) {
/* 152 */         this.m_sb.append('/');
      } else {
/* 154 */         this.m_level++;
      }
      
/* 157 */       this.m_sb.append(">\r\n");
    }
  }

  
  protected String toString(Object value) {
/* 163 */     if (value instanceof String)
/* 164 */       return (String)value; 
/* 165 */     if (value instanceof Collection) {
/* 166 */       Collection<Object> list = (Collection)value;
/* 167 */       StringBuilder sb = new StringBuilder(32);
/* 168 */       boolean first = true;

/* 170 */       for (Object item : list) {
/* 171 */         if (first) {
/* 172 */           first = false;
        } else {
/* 174 */           sb.append(',');
        }
        
/* 177 */         if (item != null) {
/* 178 */           sb.append(item);
        }
      }
      
/* 182 */       return sb.toString();
/* 183 */     }  if (value.getClass().isArray()) {
/* 184 */       int len = Array.getLength(value);
/* 185 */       StringBuilder sb = new StringBuilder(32);
/* 186 */       boolean first = true;

/* 188 */       for (int i = 0; i < len; i++) {
/* 189 */         Object item = Array.get(value, i);

/* 191 */         if (first) {
/* 192 */           first = false;
        } else {
/* 194 */           sb.append(',');
        }
        
/* 197 */         if (item != null) {
/* 198 */           sb.append(item);
        }
      }
      
/* 202 */       return sb.toString();
    }
    
/* 205 */     return String.valueOf(value);
  }
  
  protected void tagWithText(String name, String text, Object... nameValues) {
/* 209 */     if (text == null) {
      return;
    }
    
/* 213 */     indent();

/* 215 */     this.m_sb.append('<').append(name);

/* 217 */     int len = nameValues.length;

/* 219 */     for (int i = 0; i + 1 < len; i += 2) {
/* 220 */       Object attrName = nameValues[i];
/* 221 */       Object attrValue = nameValues[i + 1];

/* 223 */       if (attrValue != null) {
/* 224 */         this.m_sb.append(' ').append(attrName).append("=\"").append(escape(attrValue)).append('"');
      }
    }
    
/* 228 */     this.m_sb.append(">");
/* 229 */     this.m_sb.append(escape(text, true));
/* 230 */     this.m_sb.append("</").append(name).append(">\r\n");
  }
  
  protected void element(String name, String text, String defaultValue, boolean escape) {
/* 234 */     if (text == null || text.equals(defaultValue)) {
      return;
    }
    
/* 238 */     indent();

/* 240 */     this.m_sb.append('<').append(name).append(">");

/* 242 */     if (escape) {
/* 243 */       this.m_sb.append(escape(text, true));
    } else {
/* 245 */       this.m_sb.append("<![CDATA[").append(text).append("]]>");
    }
    
/* 248 */     this.m_sb.append("</").append(name).append(">\r\n");
  }
  
  protected String toString(Date date, String format) {
/* 252 */     if (date != null) {
/* 253 */       return (new SimpleDateFormat(format)).format(date);
    }
/* 255 */     return null;
  }


  
  public void visitMatrix(Matrix matrix) {
/* 261 */     startTag("matrix", null, new Object[] { "type", matrix.getType(), "name", matrix.getName(), "count", Integer.valueOf(matrix.getCount()), "totalTime", Long.valueOf(matrix.getTotalTime()), "url", matrix.getUrl() });

/* 263 */     if (!matrix.getRatios().isEmpty()) {
/* 264 */       for (Ratio ratio : matrix.getRatios().values()) {
/* 265 */         ratio.accept(this.m_visitor);
      }
    }
    
/* 269 */     endTag("matrix");
  }

  
  public void visitMatrixReport(MatrixReport matrixReport) {
/* 274 */     startTag("matrix-report", null, new Object[] { "domain", matrixReport.getDomain(), "startTime", toString(matrixReport.getStartTime(), "yyyy-MM-dd HH:mm:ss"), "endTime", toString(matrixReport.getEndTime(), "yyyy-MM-dd HH:mm:ss") });

/* 276 */     if (!matrixReport.getDomainNames().isEmpty()) {
/* 277 */       for (String domain : matrixReport.getDomainNames()) {
/* 278 */         tagWithText("domain", domain, new Object[0]);
      }
    }
    
/* 282 */     if (!matrixReport.getMatrixs().isEmpty()) {
/* 283 */       for (Matrix matrix : matrixReport.getMatrixs().values()) {
/* 284 */         matrix.accept(this.m_visitor);
      }
    }
    
/* 288 */     endTag("matrix-report");
  }


  
/* 293 */   public void visitRatio(Ratio ratio) { startTag("ratio", true, null, new Object[] { "type", ratio.getType(), "min", Integer.valueOf(ratio.getMin()), "max", Integer.valueOf(ratio.getMax()), "totalCount", Integer.valueOf(ratio.getTotalCount()), "totalTime", Long.valueOf(ratio.getTotalTime()), "url", ratio.getUrl() }); }
}


/* Location:              /Users/hanfei/Documents/apache-tomcat-9.0.24/webapps/cat/WEB-INF/lib/cat-consumer-3.0.0.jar!/com/dianping/cat/consumer/matrix/model/transform/DefaultXmlBuilder.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       1.0.7
 */